home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / MSC2BC.ARJ / BFARHEAP.C < prev    next >
C/C++ Source or Header  |  1992-01-22  |  7KB  |  224 lines

  1. /* BFARHEAP.C - Ffar heap example #2.
  2.    illustrates Borland C++ 3.0 far heap memory management.
  3.    It implements the minimum number of changes to MFARHEAP.C
  4.    to compile and run with Borland C++ 3.0. It allocates,
  5.    mainpulates, and deallocates two sets of far heap memory
  6.    blocks, and shows some of the details about how far heap
  7.    management works.
  8.  
  9.    Copyright (c) 1991 Borland International. All rights reserved.
  10.  
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <conio.h>
  15. #include <malloc.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. /* Note 1 - Define Borland C++3.0 functions, struct, and variables
  19.    for equivalent or near-equivalent Microsoft C6.0a items.
  20. */
  21. #define _fheapchk  farheapcheck
  22. #define _fheapset  farheapfillfree
  23. #define _fheapwalk farheapwalk /* but farheapwalk walks only the blocks used */
  24. #define _heapinfo  farheapinfo
  25. #define _useflag   in_use      /* farheapwalk returns different values */
  26. #define _size      size        /* farheapinfo defines a long unsigned int */
  27. #define _pentry    ptr
  28. /* Borland C++ 3.0 _fmalloc does not allocate a block of zero bytes, whereas
  29.    Microsoft C6.0a _fmalloc does.
  30. */
  31.  
  32. /* Function prototypes and manifest constants */
  33. void heapdump( char fill );
  34. void heap_status( int status );
  35. #define IMAX 10 /* number of heap blocks to allocate */
  36.  
  37. void main()
  38. {
  39.    void _far *p[10];
  40.    /* Note 2 - The hblocksize array keeps track of the current
  41.       size of each heap block pointer in p. */
  42.    int hblocksize[10]; /* Size of each heap block when allocated */
  43.    int i, n_alloc;
  44.  
  45.     /* Fill all current heap blocks with known data. */
  46.     heap_status( _fheapset( 254 ) );
  47.  
  48.     printf ("Free heap has been initialized.\n");
  49.  
  50.     /* Check heap consistency and dump it before doing anything else. */
  51.     heapdump( (char)254 );
  52.  
  53.     /* Check heap status. */
  54.     heap_status( _fheapchk() );
  55.     printf ("Just before allocating blocks.\n");
  56.  
  57.     /* Now allocate IMAX fixed blocks of memory to affect the far heap,
  58.        and fill them with ones. */
  59.     for( i = 0; i < IMAX; i++ )
  60.     {
  61.     /* Note 3 - Calculate and save the size of each heap block. */
  62.     hblocksize[i] = (size_t)i*10;
  63.     /* Note 4 - Borland C++ will not allow you to allocate a heap block
  64.        that is zero bytes in size.
  65.     */
  66.     if ( i != 0 )
  67.     if( (p[i] = _fmalloc( (size_t)i*10 )) != NULL )
  68.       printf( "Allocated %u bytes on far heap at %Fp\n",
  69.           hblocksize[i], p[i] );
  70.     else
  71.       break;
  72.     }
  73.     n_alloc = i-1;  /* Index of last block successfully allocated */
  74.     /* Note 5 -  The Borland C++ heap manager allocates all
  75.        unused conventional DOS memory to the far heap.  It
  76.        also concatenates adjacent free heap blocks together
  77.        to form a single contiguous heap block at the time when
  78.        a heap block is freed.
  79.     */
  80.  
  81.     printf ("Just after allocating heap blocks.\n" );
  82.     /* Fill all free heap blocks with known data. */
  83.     heap_status( _fheapset( 254 ) );
  84.     printf ("Free heap has been initialized again.\n" );
  85.  
  86.     /* Fill all allocated heap blocks with different data. */
  87.     for( i = 0; i < n_alloc; i++ )
  88.     if ( i != 0 )
  89.     _fmemset( p[i], 0xFF, hblocksize[i] );
  90.     printf ("All allocated heap blocks are now filled with 0xFF.\n" );
  91.  
  92.     /* Check heap consistency again. */
  93.     heapdump( (char)254 );
  94.  
  95.     /* Now free up the heap blocks. */
  96.     for( i=n_alloc; i >= 0; i-- )
  97.     if ( i != 0 )
  98.     {
  99.     printf( "Deallocating %u bytes from far heap at %Fp\n",
  100.         hblocksize[i], p[i] );
  101.     _ffree( p[i] );
  102.     }
  103.  
  104.     /* Check heap consistency after releasing memory. */
  105.     heapdump( (char)254 );
  106.  
  107.     /* Now AGAIN allocate IMAX fixed blocks of memory to affect
  108.        the far heap, and fill them with ones. This will see whether
  109.        free heap gets concatenated together to satisfy a request.
  110.     */
  111.     printf ("Just before allocating blocks second time.\n");
  112.     for( i = 0; i < IMAX; i++ )
  113.     {
  114.     if ( i != 0 )
  115.     if( (p[i] = _fmalloc( (size_t)i*10 )) != NULL )  /* farmalloc */
  116.       printf( "Allocated %u bytes on far heap at %Fp\n",
  117.           hblocksize[i], p[i] );
  118.     else
  119.       break;
  120.     }
  121.     n_alloc = i-1;  /* Index of last block successfully allocated */
  122.     printf ("Just after allocating heap blocks second time.\n" );
  123.     /* Fill all free heap blocks with known data. */
  124.     heap_status( _fheapset( 254 ) );
  125.     printf ("Free heap has been initialized again.\n" );
  126.  
  127.     /* Fill all allocated heap blocks with different data. */
  128.     for( i = 0; i < n_alloc; i++ )
  129.     if ( i != 0 )
  130.     _fmemset( p[i], 0, hblocksize[i] );
  131.     printf ("All allocated heap blocks are now filled with 0.\n" );
  132.  
  133.     /* Check heap consistency again. */
  134.     heapdump( (char)254 );
  135.  
  136.     /* Now free up the heap blocks. */
  137.     for( i=n_alloc; i >= 0; i-- )
  138.     if ( i != 0 )
  139.     {
  140.     printf( "Deallocating %u bytes from far heap at %Fp\n",
  141.         hblocksize[i], p[i] );
  142.     _ffree( p[i] );
  143.     }
  144.  
  145.     /* Final check of heap consistency. */
  146.     heapdump( (char)254 );
  147.  
  148.     exit(0);
  149. }
  150.  
  151. void heapdump( char fill )
  152. /* heapdump walks through all heap entries, checks consistency
  153.    of free blocks, and displays information about each heap block.
  154. */
  155. {
  156.     struct _heapinfo hi;
  157.     int hstate, i;
  158.     char _far *p;
  159.  
  160.     printf( "\n-- Current Heap Blocks ---\nStatus      Size   Address\n" );
  161.     hi.ptr = NULL;      /* Initialize to get first heap entry */
  162.     while( ( hstate = _fheapwalk( &hi )) == _HEAPOK )
  163.     {
  164.     /* Note 6 - A zero value for in_use in a farheapinfo
  165.        structure designates a heap block as used.
  166.        size is an unsigned long integer, but it reports the size of
  167.        the heap block including control information and any memory
  168.        added to make the size of the entire block an integral number
  169.        of paragraphs in size, i.e. ((size % 16) == 0).
  170.      */
  171.       printf( "%s  %10lu %Fp\n",
  172.         hi._useflag ? "USED" : "FREE",
  173.         hi._size,
  174.         hi._pentry );
  175.     }
  176.     /* Note 7 - Check that all free heap entries contain the
  177.        requested fill character.
  178.      */
  179.     hstate = farheapcheckfree(fill);
  180.     if ( hstate < 0)
  181.     switch(hstate)
  182.     {
  183.       case _HEAPCORRUPT:
  184.      printf("Heap is corrupted.\n");
  185.      break;
  186.       case _BADVALUE:
  187.      printf("There is a changed value in heap free space.\n");
  188.      break;
  189.       default:
  190.      printf("There is an unknown heap error.\n");
  191.      break;
  192.     }
  193.     else
  194.       printf ("Heap status is OK.\n");
  195.  
  196. }
  197.  
  198. void heap_status( int heapstate )
  199. /* heap_status decodes the status returned by far heap
  200.    management functions _fheapwalk, _fheapset, or _fheapchk,
  201.    and displays appropriate text.
  202. */
  203. {
  204.     /* Note 8 - All of the manifest constants used here refer
  205.        to heap error values that are common between Borland C++
  206.        and Microsoft C6.0a.
  207.      */
  208.     switch( heapstate )
  209.     {
  210.     case _HEAPOK:
  211.         printf( "Heap status is OK.\n" );
  212.         break;
  213.     case _HEAPEMPTY:
  214.         printf( "Heap status is empty.\n" );
  215.         break;
  216.     case _HEAPEND:
  217.         printf( "Heap status is at end of heap.\n" );
  218.         break;
  219.     default:
  220.         printf( "Heap status is corrupted.\n" );
  221.         break;
  222.     }
  223. }
  224.